package com.wibo.account;

import java.io.File;

import android.app.Dialog;
import android.content.ContentResolver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.preference.DialogPreference;
import android.provider.MediaStore;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class BootingImagePreference extends DialogPreference {
	private Context mContext;
	private RelativeLayout mImageLayout;
	private ImageView mLoadingImage;
	private ImageView mImageDelete;
	private String mImagePath = "";
	
	public BootingImagePreference(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
		setDialogLayoutResource(R.layout.bootingimage);
		setTitle(mContext.getString(R.string.loadingPic));
		setPositiveButtonText(mContext.getString(R.string.choosePic));
	}
	
	protected void onBindDialogView(View view) {
        super.onBindDialogView(view);  
        
        mImageLayout = (RelativeLayout) view.findViewById(R.id.imageLayout);
        mLoadingImage = (ImageView) view.findViewById(R.id.loadingImage);
        mImageDelete = (ImageView) view.findViewById(R.id.deleteImage);
        
        SharedPreferences settings = mContext.getSharedPreferences(
				WiboMainActivity.USER_INFO, 0);
        mImagePath = settings.getString(WiboMainActivity.LOADING_IMAGE_PATH, "");
        
        File file = new File(mImagePath);
        
        Uri imageUri = Uri.parse(mImagePath);
        if (imageUri == null || !file.exists()) {
        	mImageLayout.setVisibility(RelativeLayout.GONE);
        }else{
			try {
				if (imageUri != null) {
					//ContentResolver resolver = mContext.getContentResolver();
					Bitmap bitmap = BitmapFactory.decodeFile(mImagePath);
//							.decodeStream(resolver
//									.openInputStream(imageUri));

					if (bitmap != null) {
						mLoadingImage.setImageBitmap(bitmap);
						
						mImageDelete.setOnClickListener(new OnClickListener() {
							
							@Override
							public void onClick(View v) {
								mImageLayout.setVisibility(RelativeLayout.GONE);
								mImagePath = "";
								SharedPreferences settings = mContext.getSharedPreferences(
										WiboMainActivity.USER_INFO, 0);
								Editor editor = settings.edit();
								editor.putString(WiboMainActivity.LOADING_IMAGE_PATH, "");
								editor.commit();
							}
						});
					}else{
						mImageLayout.setVisibility(RelativeLayout.GONE);
					}
				}
			} catch (/*FileNotFound*/Exception e) {
				mImageLayout.setVisibility(RelativeLayout.GONE);
				e.printStackTrace();
			}
        }
	}
	
	@Override
	public void onClick(DialogInterface dialog, int which) {
		// TODO Auto-generated method stub
		switch (which) {
		case Dialog.BUTTON_POSITIVE:
			setPositiveButtonText(mContext.getString(R.string.choosePic));
			/*
			Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);  
			intent.setType("image/*");    //这个参数是确定要选择的内容为图片
			intent.putExtra("crop", "true");   //这个参数 不太懂，唯一知道的是：设置了参数，就会调用裁剪，如果不设置，就会跳过裁剪的过程。
			intent.putExtra("aspectX", 640);  //这个是裁剪时候的 裁剪框的 X 方向的比例。
			intent.putExtra("aspectY",912);  //同上Y 方向的比例. (注意： aspectX, aspectY ，两个值都需要为 整数，如果有一个为浮点数，就会导致比例失效。)
			//设置aspectX 与 aspectY 后，裁剪框会按照所指定的比例出现，放大缩小都不会更改。如果不指定，那么 裁剪框就可以随意调整了。
			intent.putExtra("outputX", 640);  //返回数据的时候的 X 像素大小。
			intent.putExtra("outputY", 912);  //返回的时候 Y 的像素大小。
			//以上两个值，设置之后会按照两个值生成一个Bitmap, 两个值就是这个bitmap的横向和纵向的像素值，如果裁剪的图像和这个像素值不符合，那么空白部分以黑色填充。
			intent.putExtra("scale", true);
			intent.putExtra("return-data", false);  //是否要返回值。 一般都要。
			intent.putExtra("output", ((WiboSettings)mContext).getTempFile());
			intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());//返回格式
			intent.putExtra("noFaceDetection", false); // 是否去除面部检测， 如果你需要特定的比例去裁剪图片，那么这个一定要去掉，因为它会破坏掉特定的比例。
*/
			((WiboSettings)mContext).setImageName("cut_" + System.currentTimeMillis() + ".jpg");
			Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
            intent.setType("image/*");
            intent.putExtra("crop", "true");
            intent.putExtra("aspectX", aspectX);
            intent.putExtra("aspectY", aspectY);
            intent.putExtra("outputX", outputX);	
            intent.putExtra("outputY", outputY);
            intent.putExtra("scale", scale);
            intent.putExtra("return-data", return_data);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, ((WiboSettings)mContext).getTempUri());
            intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
            intent.putExtra("noFaceDetection",!faceDetection); // lol, negative boolean noFaceDetection
            if (circleCrop) {
            	intent.putExtra("circleCrop", true);
            }
            
			((WiboSettings)mContext).startActivityForResult(intent, 0);

			break;
		case Dialog.BUTTON_NEGATIVE:
			break;
		}
		super.onClick(dialog, which);
	}
	
	protected int outputX = 640;
	protected int outputY = 912;
	protected int aspectX = 2;
	protected int aspectY = 3;
	protected boolean return_data = false;
	protected boolean scale = true;
	protected boolean faceDetection = true;
	protected boolean circleCrop = false;
}
